<template>
  <div class="app-container">
    <el-dialog
      :title="title2"
      :visible.sync="open2"
      width="960px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="版本编码" prop="versionsCode">
          <el-input
            v-model="queryParams.versionsCode"
            placeholder="请输入版本编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="版本名称" prop="versionsName">
          <el-input
            v-model="queryParams.versionsName"
            placeholder="请输入版本名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="versionsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="版本编码" align="center" prop="versionsCode" />
        <el-table-column label="版本名称" align="center" prop="versionsName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <!-- 添加或修改工艺路径版本对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="960px"
      append-to-body
      @close="cancel"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="版本编码" prop="versionsCode">
          <el-input v-model="form.versionsCode" placeholder="请输入版本编码" />
        </el-form-item>
        <el-form-item label="版本名称" prop="versionsName">
          <el-input v-model="form.versionsName" placeholder="请输入版本名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>

      <el-tabs type="border-card" v-if="processList.length > 0&& form.versionsId!=null ">
        <el-tab-pane
          v-for="(process, index) in processList"
          :key="index"
          :label="process.processName"
        >
          <RouteproductMain
            ref="productbom"
            :routeId="form.routeId"
            :recordId="form.recordId"
            :versionsId="form.versionsId"
            :processName="process.processName"
            :processCode="process.processCode"
            :processId="process.processId"
            :optType="optType"
            :bomItemCode="bomItemCode"
            :itemId="itemId"
            @backEndData="getSubassembly"
            :subassemblyList="subassemblyList"
          ></RouteproductMain>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import RouteproductMain from "./components/routeproductbom.vue";
import {
  listVersions,
  getVersions,
  delVersions,
  addVersions,
  updateVersions,
  getProcesses
} from "@/api/system/versions";

export default {
  name: "Versions",
  data() {
    return {
      //组件是否展示
      open2: false,
      //组件的标题
      title2: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺路径版本表格数据
      versionsList: [],
      //当前工艺中配置的工序清单
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        routeId: null,
        recordId: null,
        versionsCode: null,
        versionsName: null,
        status: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeId: [
          { required: true, message: "工艺路线ID不能为空", trigger: "blur" }
        ]
      },
      //bom版本编号
      bomItemCode: "",
      //bom版本id
      itemId: "",
      recordId: "",
      //子组件传过来的数据
      subassemblyList: []
    };
  },
  components: { RouteproductMain },
  props: {
    routeId: undefined,
    optType: undefined
  },
  created() {
    this.getList();
  },
  methods: {
    //获取子组件数据
    getSubassembly(msg, type) {
      if (type == "add") {
        msg.forEach(item => {
          if (item != undefined) {
            this.subassemblyList.push(item);
          }
        });
        console.log(this.subassemblyList, "祖父组件数据");
      } else if (type == "del") {
        this.subassemblyList = this.subassemblyList.filter(
          _ => _.itemCode != msg.itemCode
        );
        console.log(this.subassemblyList, "祖父组件数据11111");
      }
    },
    init(row) {
      this.form = row;
      this.open2 = true;
      this.title2 = "工艺路线管理";
      this.bomItemCode = row.bomItemCode;
      this.itemId = row.itemId;
      this.queryParams.recordId = row.recordId;
      this.recordId = row.recordId;
      this.form.recordId = this.recordId;
      this.getList();
    },
    /** 查询工艺路径版本列表 */
    getList() {
      this.loading = true;
      listVersions(this.queryParams).then(response => {
        this.versionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        versionsId: null,
        //routeId: null,
        versionsCode: null,
        versionsName: null,
        status: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.subassemblyList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.versionsId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工艺路径版本";
      this.form.routeId = this.routeId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改工艺路径版本";
      this.form.routeId = this.routeId;

      getProcesses(this.form.routeId).then(res => {
        this.processList = res.data;
        this.$nextTick(() => {
          console.log(this.$refs.productbom, "1111111111111111======1111111");
          for (let i = 0; i < this.$refs.productbom.length; i++) {
            const element = this.$refs.productbom[i];
            element.init();
          }
          // this.$refs.productbom.init(row);
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.versionsId != null) {
            this.form.proRouteVersionsBomList = this.subassemblyList;
            delete this.form.auditStatus;
            this.form["bomItemCode"] = this.bomItemCode;
            updateVersions(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.recordId = this.recordId;
            addVersions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const versionsIds = row.versionsId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除工艺路径版本编号为"' + versionsIds + '"的数据项？'
        )
        .then(function() {
          return delVersions(versionsIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/versions/export",
        {
          ...this.queryParams
        },
        `versions_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
  